$(document).ready(function() {	
	$.ajaxSetup({timeout:5000}); 
	//重置查询条件
	$("#q_rest").click(function(){
		document.getElementById("eventLevel").selectedIndex=0;
		document.getElementById("eventStatus").selectedIndex=0;
		document.getElementById("eventType").selectedIndex=0;
		document.getElementById("eventSource").selectedIndex=0;
		document.getElementById("order").selectedIndex=0;
		$("#eId").val("");
		$("#eventTimeStart").val("");
		$("#eventTimeEnd").val("")
	});
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventLevel.action",
  		success:function(data){
  			$("<option value='0'>所有</option>").appendTo("#eventLevel");
  			$.each(data.eventLevels,function(i,n){
  				$("<option value="+n.elId+">"+n.elEventLevel+"</option>").appendTo("#eventLevel");
			});
  		}
	});
	
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventStatus.action",
  		success:function(data){
  			//$("#eState").append("<span>事件状态：</span>");
  			$("<option value='0'>所有</option>").appendTo("#eventStatus");
  			$.each(data.eventStatuses,function(i,n){
//  				if(n.tbesId=='1001'||n.tbesId=='1002'||n.tbesId=='1003'){
//  					var strHtml="<span class='eventState'><input value='"+n.tbesId+"' type='checkbox' checked >"+n.tbesEventStatus+"</input></span>";
//  					$("#eState").append(strHtml);
//  				}
  				$("<option value="+n.tbesId+">"+n.tbesEventStatus+"</option>").appendTo("#eventStatus");
			});
  		}
	});	
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventType.action",
  		success:function(data){
  			$("<option value='0'>所有</option>").appendTo("#eventType");
  			$.each(data.eventTypes,function(i,n){
  				$("<option value="+n.etId+">"+n.etEventType+"</option>").appendTo("#eventType");
			});
  		}
	});
	
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventSource.action",
  		success:function(data){
  			$("<option value='0'>所有</option>").appendTo("#eventSource");
  			$.each(data.eventSources,function(i,n){
  				$("<option value="+n.esId+">"+n.esEventSource+"</option>").appendTo("#eventSource");
			});
  		}
	});
	
	$.ajax({
		type : "post",
		data:{"queryOption['order']":'0'},
		url: "wzp/query_queryByUser.action",
		success : showData 
	});
	
	function getParams(p){
		$("#listEvent").empty();
		$("#loadingEvent").show();
		var page=p;
		var maxRow=$("#maxRow").text();
		var elId=$.trim($("#eventLevel option:selected").val());
		var tbesId=$.trim($("#eventStatus option:selected").val());
		var etId=$.trim($("#eventType option:selected").val());
		var esId=$.trim($("#eventSource option:selected").val());
		var order=$.trim($("#order option:selected").val());
		var EId=$.trim($("#eId").val());
		var eventTimeStart=$.trim($("#eventTimeStart").val());
		var eventTimeEnd=$.trim($("#eventTimeEnd").val());
//		
//		var checktbesId = $("input:checked");
//		var tbesIds = new Array();
//		if(checktbesId.length>0){
//			for(var i = 0;i < checktbesId.length; i++ ){
//				var value=$(checktbesId[i]).val();
//				tbesIds.push(value);
//			}
//		}else{
//			//tbesIds = new Array('1001','1002','1003').toString()
//		}
		
		params = {
				"queryOption['page']":page,
				"queryOption['maxRow']":maxRow,
		 		"queryOption['elId']":elId,
				"queryOption['tbesId']":tbesId,
				"queryOption['etId']":etId,
				"queryOption['esId']":esId,
				"queryOption['EId']":EId,
				"queryOption['eventTimeStart']":eventTimeStart,
				"queryOption['eventTimeEnd']":eventTimeEnd,
				"queryOption['order']":order
				};
		return params;
	}
	//分页查询，异步提交
	function doAjax(d){
		$.ajax({
			type : "post",
			url: "wzp/query_queryByUser.action",
			data : d,
			success : showData
		});
	}
  	
	//根据条件查询 
	$("#query").click(function(){
		$("#eventTable").fadeIn("fast");
		doAjax(getParams(1));
	});
	//分页查询，查第一页
	$("#firstPage").click(function(){
		doAjax(getParams(1));
	});
	//分页查询，查上一页
	$("#prePage").click(function(){
		var pageCount=$("#pageCount").text();
		var page=$("#page").text();
		page=(parseInt(page)-1);
		if(page<=0){
			alert("这已经是第一页了，没有上一页！");
			return;
		}
		doAjax(getParams(page));
	});
	//分页查询，查下一页
	$("#nextPage").click(function(){
		var pageCount=$("#pageCount").text();
		var page=$("#page").text();
		page=(parseInt(page)+1);
		if(page>pageCount){
			alert("这已经是最后一页了，没有下一页！");
			return;
		}
		doAjax(getParams(page));
	});
	//分页查询，查最后一页
	$("#lastPage").click(function(){
		var page=$("#pageCount").text();
		doAjax(getParams(page)); 
	});
	//分页查询，跳转到第n页
	$("#jumpPage").click(function(){
		var pageCount=$("#pageCount").text();
		//alert(pageCount);
		var page=$("#jump").val();
		//alert(page);
		if(parseInt(page, 10) > parseInt(pageCount, 10)){
			alert("要跳转的页面超出范围！");
			return;
		} 
		doAjax(getParams(page)); 
	});
	
	function showData(data){
		$("#listEvent").empty();
		//alert(data.currentUid);
		//alert(data.ids);
		if(data.pageUtil==null||data.ajaxRst!="true"){
			$("#loadingEvent").hide();
			$("#noneEvent").show();
			$("#total").text(0);
			$("#page").text(0);
			$("#pageCount").text(0);
			$("#maxRow").text(0);
			alert("查询失败，请重新登录！");
		}
		else if(data.pageUtil.list==null||data.pageUtil.list==""){
			$("#loadingEvent").hide();
			$("#noneEvent").show();
			$("#total").text(0);
			$("#page").text(0);
			$("#pageCount").text(0);
			$("#maxRow").text(0);
		}else{
			$("#listEvent").hide();
			$("#noneEvent").hide();
		    $("#loadingEvent").hide();
		    $.each(data.pageUtil.list,function(i,n){
				fillDataToHtml(n.EId, n.tbEventLevel.elEventLevel,n.tbEventLevel.elColor,n.EEventtime,
						  n.EEventAddr, n.EEventname,n.EAcceptTime,n.tbEventType.etEventType,
						  n.tbEventStatus.tbesEventStatus,n.tbEventStatus.tbesColor,data.ids);
				$("#total").text(data.pageUtil.total);
				$("#page").text(data.pageUtil.page);
				$("#pageCount").text(Math.ceil(data.pageUtil.total/10));
				$("#maxRow").text(data.pageUtil.maxRow);
		  	});
		$("#listEvent").show();
		}
	}
	
	
	function imgchange(e_level){//判断等级，显示图标
		var tmp;
		switch(e_level)
		{
		case "未定级": tmp="\\ag\\wzp\\images\\t_wdj.png"; break;
		case "一般（IV级）": tmp="\\ag\\wzp\\images\\t_yb.png";break;
		case "较重（III级）": tmp="\\ag\\\wzp\\images\\t_jz.png";break;
		case "严重（II级）": tmp="\\ag\\wzp\\images\\t_yz.png";break;
		case "特别严重（I级）": tmp="\\ag\\wzp\\images\\t_tbyz.png";break;
		default: tmp="default";

		}
		return tmp;
		
	}
	
	function fillDataToHtml(eid,eventLevel,elColor,eventTime,eventAddr,eventName,acceptTime,eventType,eventStatus,tbesColor,ids){
		var strHtml="<tr>";
	    var i_change;
		i_change=imgchange(eventLevel);//判断等级显示图标
        strHtml+="<td ><div><img src=\""+i_change+"\"/><a class='"+eid+"' href='javascript:;'>"+eid+"</a></div></td>";
        
        strHtml+="<td class='_"+eid+"'><div>"+eventName+"</div></td>";
        strHtml+="<td ><div style='color:#"+elColor+"'>"+eventLevel+"</div></td>";
        strHtml+="<td class='_"+eid+"'><div>"+eventTime.replace(/T/," ")+"</div></td>";
        strHtml+="<td class='_"+eid+"'><div>"+eventAddr+"</div></td>";
        
        strHtml+="<td class='_"+eid+"'><div>"+acceptTime.replace(/T/," ")+"</div></td>";
        strHtml+="<td class='_"+eid+"'><div>"+eventType+"</div></td>";
        strHtml+="<td class='_"+eid+"'><div style='color:#"+tbesColor+"'>"+eventStatus+"</div></td>";
        strHtml+="<td class='"+eid+"'><div><input name='read' type='button' value='查看' /></div></td>";
        strHtml+="</tr>";
        //onclick='window.location.href=\"/ag/wzp/event_findById.action?event.EId="+eid+"\"'
        $("#listEvent").append(strHtml);
        var id = "."+eid;
        var kk = "._"+eid;
        $(id).click(function(){
        	readDetail(eid);
        });
	    $.each(ids,function(i,n){
	    	if(eid==n){
	    		$(kk).css("color","#0066CC");
	    	}
	  	});
        
        
        
        
        function readDetail(eid){
        	//window.location.href="/ag/wzp/event_findById.action?event.EId="+eid;
			//alert("dfdsfsdyyyyyyyyyyyyyyyyyyy");
			//alert($('#centerFrameSet', window.parent.document).attr("rows"));
			setCookie(eventId,eid);
			//findEventStatusAjax(eid);
			//alert(eid);
        	//$('#topFrame', window.parent.document).attr("src", "/ag/work/event_manage_top.jsp");
			//$('#mainFrame', window.parent.document).attr("src", "");
			//$('#mainFrame', window.parent.document).attr("src", "/ag/wzp/event_findById.action?event.EId="+eid);
			/*
			var par=$('#mainFrame', window.parent.document).parent();
			$('#mainFrame', window.parent.document).remove();
			var html1="<frame src='/ag/wzp/event_findById.action?event.EId="+eid+"' name='mainFrame' noresize='noresize'"+
				"id='mainFrame' style='border-left: solid 1px #84b1d3;'>"
			par.append(html1);
			alert(par.html());*/
			//window.parent.frames['mainFrame'].location.href="/ag/work/event_manage_top.jsp";
			//window.location.replace("/ag/work/event_manage_top.jsp");
			//$('#centerFrameSet', window.parent.document).attr("rows", "75,6,*"); 
			//redirectPage("/ag/wzp/event_findById.action?event.EId="+eid);
			
        	//$('#mainFrame', window.parent.document).attr("src", "/ag/wzp/event_findById.action?event.EId="+eid);
			//alert("gggggggggg123");
			//window.location.href="/ag/work/event_man.jsp";
			//goUrl("/ag/work/event_man.jsp");
			//var aa="/ag/wzp/event_findById.action?event.EId=";	
			//$('#mainFrame').attr("src", aa+getCookie(eventId));
			//window.parent.frames['leftFrame']
			$('#myEvent', window.parent.frames['leftFrame'].document).show();
			var topp=$('#myEvent', window.parent.frames['leftFrame'].document).prev();
			topp.children().find('img').attr("src","images/secondmenuheadclick.png");
			topp.children().find('a').css("color","#F90");
			
//			alert(getCookie("eventArr"));
			var roleTmp=getCookie("eventArr")+",";
			var roleArr=roleTmp.split(",");
			var goo="0";
			for(var i=0;i<roleArr.length;i++){
				if(roleArr[i]==getCookie(eventId)){
					//alert(roleArr[i]);
					goo="1";
					break;
				}
			}
			if(goo=="1"){
				$('#zhz', window.parent.frames['leftFrame'].document).show();
			}else{
				$('#zhz', window.parent.frames['leftFrame'].document).hide();				
			}
			goUrl("/ag/wzp/event_findById.action?event.EId="+getCookie(eventId));
			//alert(getCookie(eventId));
			//alert(getCookie("eventArr"));
			//alert(getCookie("UName"));
			//alert(getCookie("role"));
			//var tbUserRoles=getCookie("tbUserRoles");
			//alert(tbUserRoles.length);
			/*
			$.each(tbUserRoles,function(i,n){
				alert(n.tbRole);
				//alert(n.tbRole.RName);
			});*/
			
			
			/*
			var eventTmp=getCookie("eventArr");
			var roleArr=eventTmp.split(",");
			var goo="0";
			for(var i=0;i<roleTmp.length;i++){
				if(roleTmp[i]==getCookie(eventId)){
					goo="1";
					break;
				}
			}
			if(goo=="1"){
				//alert("ggggggggggggggg");
			}
			//alert(getCookie("eventArr"));*/
			
			
			//goUrl("/ag/wzp/event_findById.action?event.EId="+getCookie(eventId));			
			//$('#switchImg', window.parent.document).click();
			//$('#tdIframe', window.parent.parent.document).hide("slow");
			//alert($('#tdIframe', window.parent.parent.document).html());
			//$('#tdIframe', window.parent.document).hide("slow");
			//$('#tdIframe').hide("slow"); 
			//$('#mincenterFrameSet', window.parent.parent.parent.parent.document).attr("cols", "175,6,*");
			//$('#centerFrameSet', window.parent.parent.parent.document).attr("rows", "0,6,*"); 
        	//window.location.href
        }	
		function goUrl(x)
{
     window.location.href=x;
}
        
	}
	
});